package com.sibu.yx.promotion.admin.web.support.filter;

import com.doubo.json.util.JsonUtil;
import com.sibu.yx.promotion.common.manage.ResponseManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;


/**
 * 退出过滤器：退出成功返回json格式
 * @author zhengsu
 */
@Slf4j
public class CustomLogoutFilter extends LogoutFilter {
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) {
        Subject subject = getSubject(request, response);
        try {
            subject.logout();
        } catch (SessionException ise) {
            log.debug("Encountered session exception during logout.  This can generally safely be ignored.", ise);
        }
        return false;
    }

    @Override
    protected void postHandle(ServletRequest request, ServletResponse servletResponse) throws IOException {
        servletResponse.setContentType("application/json;charset=UTF-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.getWriter().write(JsonUtil.toJson(ResponseManage.success("退出成功")));

    }

}
